﻿Imports System.Data.SqlClient

Public Class frm_roomEdit

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub
    Public Sub LoadData()
        Dim cn As New SqlConnection("SERVER = .; USER = sa; PASSWORD = 123; DATABASE = HOTELAPP;")
        cn.Open()
        Dim cmd As New SqlCommand("SELECT tr.RoomID, rt.RoomType, rp.RoomPrice, tr.Floor, tr.RoomAdditional FROM TblRooms as tr, TblRoomType as rt, TblRoomPrice as rp WHERE tr.RoomTypeID = rt.RoomTypeID AND tr.RoomPriceID = rp.RoomPriceID", cn)

        Dim reader = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(reader)
        reader.Close()

    End Sub

    Private Sub frm_roomEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim cn As New SqlConnection("server = .; user = sa; password = 123; database = HOTELAPP")
        cn.Open()
        Dim cmd As New SqlCommand("SELECT * FROM TblRoomType", cn)
        Dim dr = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(dr)
        dr.Close()

        Me.cob_r_type.DisplayMember = "RoomType"
        Me.cob_r_type.ValueMember = "RoomTypeID"
        Me.cob_r_type.DataSource = dt
        Me.cob_r_type.SelectedIndex = -1

        Dim cmds As New SqlCommand("SELECT * FROM TblRoomPrice", cn)
        Dim drs = cmds.ExecuteReader()
        Dim dtt As New DataTable
        dtt.Load(drs)
        drs.Close()

        Me.cob_r_price.DisplayMember = "RoomPrice"
        Me.cob_r_price.ValueMember = "RoomPriceID"
        Me.cob_r_price.DataSource = dtt
        Me.cob_r_price.SelectedIndex = -1

    End Sub

    
    Private Sub btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
        Try
            If txt_code.Text = "" Then
                Dim cn As New SqlConnection
                cn.ConnectionString = "server = .; user = sa; password = 123; database = HOTELAPP"
                cn.Open()
                'MessageBox.Show("connected")
                Dim cmd As New SqlCommand()
                cmd.Connection = cn
                cmd.CommandText = "INSERT INTO TblRooms (RoomID, RoomTypeID, RoomPriceID, Floor, RoomAdditional) VALUES(N'', N'" & cob_r_type.SelectedValue & "', N'" & cob_r_price.SelectedValue & "', N'" & txt_floor.Text & "', N'" & txt_des.Text & "' )"
                'Dim cmd As New SqlCommand("INSERT INTO TblRooms VALUES(N'', N'" & cob_r_type.Text & "', N'" & cob_r_price.Text & "', N'" & txt_floor.Text & "', N'" & txt_des.Text & "' )")
                cmd.ExecuteNonQuery()

            Else
                Dim cn As New SqlConnection
                cn.ConnectionString = "server = .; user = sa; password = 123; database = HOTELAPP"
                cn.Open()
                'MessageBox.Show("connected")
                Dim cmdup As New SqlCommand()
                cmdup.Connection = cn
                cmdup.CommandText = "UPDATE TblRooms SET RoomTypeID = N'" & cob_r_type.SelectedValue & "', RoomPriceID =N'" & cob_r_price.SelectedValue & "', Floor =N'" & txt_floor.Text & "', RoomAdditional =N'" & txt_des.Text & "' where RoomID = N'" & txt_code.Text & "' "
                cmdup.ExecuteNonQuery()


            End If
            Me.DialogResult = Windows.Forms.DialogResult.OK

        Catch ex As Exception
            MessageBox.Show(ex.ToString)

        End Try

    End Sub
End Class